package com.baiducloud.dawnoct.testsdk;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

import static com.baiducloud.dawnoct.testsdk.RSAUtils.ECB_PKCS1_PADDING;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdk);
    }
    //发码方最终签名

    String signatureFinal = "268081F2A8451320C31BC0CF8C156A97EFFA8E8E3AA2222E0A4E9FCF358FAC395FA03729704646581895BD07FBE4AB4D0E4F6F7688588944F009D331A2A2A4FBAE88C6034846C8FD3FDD68C7CCBDD026048CC8BDA3A58324A72E3E1CF27B26F7DB2A5A1523A61A3A87ABED0B0FD02CDAF4B3AB368F8F6C46E3CB44609ACF5CAB81FDBE5C0B4EED713365F866D86ECB2EDE03E532E2399484939909";
    //加了00后变为正数
    String N = "00BBBC49996CB668B80A83E6BD5DC98937334315E46AD46C09DAEBDF7E57FFAA30FB9C7D130E87CA06BED60E07648F0109D117A97F0D406DCC5D1F5FE63315151F2A5221B054474E16166F64E76053FF8C5C7A8C950109E3F820ED0505D7A4C7B4F24FD57EC09F8052E35A84255EEEF693A51BDC1819C29ECF2D439AE48CA08657";
    String E = "00010001";
    String D = "4DC9F5872CAC22950BCA5ECC1D75FA34D4B959F3652EB2BB9CAA3AD79BB7F4B9CA302C053EF2960C187C12A10E9250C05412E5691F41109DEB022A96F498AA3B0A91D686CDE8CD9A3CADF5D3CAAB59BE5BDD05B08FF25A70E900A64515F08FFFAC9900B2DE4F50691E39883A444DAC661DB248865B8E49F37FFFB0FD6C0A003D";
    //明文signature1
    String signature1_plain = "6A01020304050607080900000000010000000101020304050607080900FFFFFFFFFFFFFFFF00A8DA1916CB48FE8D1C0042A2209186F7AF3DF951A8E99D1595CC075125165825670EAB5113C559616E2ADFC1F53214516E02BABA786B5F57E7777C84AEF9AD7B8615B74C6D3238AE7D35912674AF7CA9F998FC3803A7EC8DBCBC";

    public void sdk(View view) {
        //todo 需要有app传进来的参数有,
        // todo 发码方数据签名signatureFinal, 发码方的公钥(绝非证书里的公钥)模数N和专用指数E(16进制字符串)
        byte[] bytes = HexUtils.hexStringToBytes(signatureFinal);
        byte[] bytes1 = subBytes(bytes, 0, bytes.length - 27);
        // 密文signatur1
        String signatur1 = HexUtils.bytesToHexString(bytes1);
        //使用发码方的公钥解密(N,E 参数由app传来)
        RSAPublicKey rsaPublicKey = RSAUtils.getRSAPublicKey(N, E);
        //使用公钥解密
        try {
            //密文是128个字节, 解密时不需要填充
            byte[] bytes2 = RSAUtils.decryptByPublicKeyForSpilt(HexUtils.hexStringToBytes(signatur1), rsaPublicKey.getEncoded());
            //获得signatur1的明文
            String signature1_plain = HexUtils.bytesToHexString(bytes2);
            //todo 保存明文, 解析明文,保存需要的数据,如有效期
            if (bytes2.length == 128) {
                byte[] bytesValidTime = subBytes(bytes2, 30, 4);
                String s = HexUtils.bytesToHexString(bytesValidTime);
                //变换为十进制
                Long validTime = Long.parseLong(s, 16);
                String ff = "";
            }
            String ff = "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ff = "";
    }

    public void uerSign(View view) {
        //发码方签名数据
        String signatureFinal = "268081F2A8451320C31BC0CF8C156A97EFFA8E8E3AA2222E0A4E9FCF358FAC395FA03729704646581895BD07FBE4AB4D0E4F6F7688588944F009D331A2A2A4FBAE88C6034846C8FD3FDD68C7CCBDD026048CC8BDA3A58324A72E3E1CF27B26F7DB2A5A1523A61A3A87ABED0B0FD02CDAF4B3AB368F8F6C46E3CB44609ACF5CAB81FDBE5C0B4EED713365F866D86ECB2EDE03E532E2399484939909";
        //用户源数据
        String userData = "00000000FFFF000000000000000001017FFFFFFF010203040506070809000102030405060708090001020304050607080900";
        //将两者合算HASH
        //常用算法：MD5、SHA、CRC
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] decode = HexUtils.hexStringToBytes(signatureFinal + userData);
        byte[] result = digest != null ? digest.digest(decode) : new byte[0];
        //发码方签名数据+用户源数据的HASH值
        String hash1 = HexUtils.bytesToHexString(result);
        String hash1Upper = hash1 != null ? hash1.toUpperCase() : null;
        //用户源数据+Hash值
        String beforPadding = userData + hash1Upper;
        //获得文档中的ISO 9796 padding bytes填充数据
        String padded = getPaddingString(96);//用户签名数据的长度为90
//        int lll = HexUtils.hexStringToBytes(padded).length;
        //填充完毕的数据(加密前的用户签名数据)
        String pddingFinishUserData = "4B" + padded + beforPadding + "BC";
//        int fff = HexUtils.hexStringToBytes(pddingFinishUserData).length;
        //todo 生成用户签名数据(需要获取用户私钥,其实是存储在本地的)
        //用户密钥的模系数 签名加了00  //加了00后变为正数
        String N1 = "00A8DA1916CB48FE8D1C0042A2209186F7AF3DF951A8E99D1595CC075125165825670EAB5113C559616E2ADFC1F53214516E02BABA786B5F57E7777C84AEF9AD7B8615B74C6D81FDBE5C0B4EED713365F866D86ECB2EDE03E532E2399484939909";
        //用户公钥指定系数
        String E = "00010001";
        //用户私钥指定系数
        String D1 = "029A06AF8559C0F352BD15C9BFF8F76489AE9450D13FEA927E87568DB0BB0F080CD615916C9DAFDE7FE72E4C03A4F3849498BFD61EB2CCE51EC780762B3E239BEF5B8F34B29944484A248E7894E5A86DE4850A121CA0332EAEE3599AFF414711";
        //生成用户私钥
        RSAPrivateKey rsaPrivateKey = RSAUtils.getRSAPrivateKey(N1, D1);
        try {
            int ffff = HexUtils.hexStringToBytes(pddingFinishUserData).length;
            byte[] bytes = RSAUtils.encryptByPrivateKeyForSpilt02
                    (HexUtils.hexStringToBytes(pddingFinishUserData), rsaPrivateKey);
            int bb = bytes.length;
            //最终用户签名数据
            String userSignFinal = HexUtils.bytesToHexString(bytes);

            String gg = "";

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @NonNull
    private String getPaddingString(int lenght) {
        int j = lenght - 72;
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < j; i++) {
            buffer.append("BB");
        }
        return buffer.toString();
    }

    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }
}
